package database;

import entity.PointType;
import manager.PointTypeManager;
import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Noi
 */
public class PointTypeManagerDB {

    private CallableStatement cstmt;
    private java.sql.Connection conn;
    private ResultSet rs;

    public PointTypeManagerDB(java.sql.Connection conn) {
        this.conn = conn;
    }

    public void loadData(PointTypeManager pointTypeManager) {
        try {
            cstmt = conn.prepareCall("{call sp_GetPointType}");
            rs = cstmt.executeQuery();
            while (rs.next()) {
                int pointTypeID = rs.getInt("PointTypeID");
                String pointTypeName = rs.getString("PointValue");
                boolean status = rs.getBoolean("Status");
                PointType obj = new PointType(pointTypeID, pointTypeName, status);
                pointTypeManager.add(obj);
            }
            rs.close();
            cstmt.close();
        } catch (SQLException ex) {
            Logger.getLogger(PointTypeManagerDB.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public void deletePointType(PointType obj) {
        try {
            cstmt = conn.prepareCall("{call sp_DeletePointType(?)}");
            cstmt.setInt(1, obj.getPointTypeID());
            cstmt.executeUpdate();
            cstmt.close();
        } catch (SQLException ex) {
            Logger.getLogger(PointTypeManagerDB.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public void insertPointType(PointType obj) {
        try {
            cstmt = conn.prepareCall("{call sp_InsertPointType(?,?)}");
            cstmt.registerOutParameter(1, Types.INTEGER);
            cstmt.setString(2, obj.getPointName());
            cstmt.executeUpdate();
            obj.setPointTypeID(cstmt.getInt("PointTypeID"));
            cstmt.close();
        } catch (SQLException ex) {
            Logger.getLogger(PointTypeManagerDB.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public void updatePointType(PointType obj) {
        try {
            cstmt = conn.prepareCall("{call sp_UpdatePointType(?,?,?)}");
            cstmt.setInt(1, obj.getPointTypeID());
            cstmt.setString(2, obj.getPointName());
            cstmt.setBoolean(3, obj.isStatus());
            cstmt.executeUpdate();
            cstmt.close();
        } catch (SQLException ex) {
            Logger.getLogger(ProvinceManagerDB.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
}
